{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f0457b74",
   "metadata": {},
   "source": [
    "1.读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2f02663b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f01d3bb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1         0       3   \n",
       "1            2         1       1   \n",
       "2            3         1       3   \n",
       "3            4         1       1   \n",
       "4            5         0       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
       "4                           Allen, Mr. William Henry    male  35.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  \n",
       "3      0            113803  53.1000  C123        S  \n",
       "4      0            373450   8.0500   NaN        S  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('C:/Users/lenovo/BigData/PythonBase/titanic_trains.csv') #路径：绝对路径\n",
    "df = pd.read_csv('./titanic_trains.csv') #相对路径都支持,可以按快捷键tab补全路径\n",
    "df.head() #查看数据的前5行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f24ebbdf",
   "metadata": {},
   "source": [
    "2缺失值的处理-删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "19f9faca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PassengerId      0\n",
       "Survived         0\n",
       "Pclass           0\n",
       "Name             0\n",
       "Sex              0\n",
       "Age            177\n",
       "SibSp            0\n",
       "Parch            0\n",
       "Ticket           0\n",
       "Fare             0\n",
       "Cabin          687\n",
       "Embarked         2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按列删除(遵循80%法则)如果一列的非缺失部分低于80%则可以考虑删除该列，也就是缺失部分超过20%\n",
    "#axis按列还是行删除 how全部删除还是部分删除 thresh(非缺失值部分数据是多少，891是数据行，0.8是非缺失值占比)，inplace参数表示是否对原始原始\n",
    "#变量进行修改，Ture修改有返回值，false表示不修改并且没有返回值\n",
    "# df_drop = df.dropna(axis='columns',how='any',thresh=891*0.8) #3.10版本不支持how和thresh参数同时出现\n",
    "df_drop = df.dropna(axis='columns',thresh=891*0.8,inplace=False) #axis也可以是1，0，1代表列，0代表行\n",
    "#删掉缺失值占比超过20%的列之后，再看占比情况\n",
    "df_drop.isnull().sum()/df.shape[0]*100\n",
    "df.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a3b650c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PassengerId    0.0\n",
       "Survived       0.0\n",
       "Pclass         0.0\n",
       "Name           0.0\n",
       "Sex            0.0\n",
       "Age            0.0\n",
       "SibSp          0.0\n",
       "Parch          0.0\n",
       "Ticket         0.0\n",
       "Fare           0.0\n",
       "Embarked       0.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#按行删除\n",
    "df_drop_row = df_drop.dropna(axis=0,how='any')#按行删除时不能指定thresh参数\n",
    "df_drop_row.isnull().sum()/891*100  #这样删完以后，整个df_drop_row一个缺失值都没有了"
   ]
  },
  {
   "cell_type": "raw",
   "id": "48ccecb6",
   "metadata": {},
   "source": [
    "3.特征缩放:处理的都是数值类型的数据\n",
    "    作用：数据中的属性取值范围不同，可能存在取值范围差异过大的情况。这时候建模可能就会出现降低了某个取值范围较小的属性对于模型的重要性。所以就需要通过特征缩放来平衡属性之间的重要性(权重/贡献度)。同时还可以降低属性的取值范围以提高模型的建模速度(建模效率/模型收敛速度)。提高模型精度\n",
    "    实现方法：\n",
    "        标准化：\n",
    "            原理：将数据缩放到均值为0方差为1的状态\n",
    "            特点：\n",
    "                1.数据最好符合高斯分布、越符合高斯分布效果越好\n",
    "                2.不改变数据的分布状态。适合用于涉及到与距离相关的场景中，比如KNN、Kmeans算法就需要做特征缩放\n",
    "                3.在做标准化的前提是数据必须要进行缺失值的处理，也就是要没有缺失值\n",
    "        最小值-最大值归一化：\n",
    "            原理：将数据缩放到0到1或者-1到1之间\n",
    "            特点：\n",
    "                1.不要求数据满足高斯分布。(优点)\n",
    "                2.会改变数据的分布状态，不适合涉及到与距离计算的场景中。（缺点）\n",
    "                3.会受到异常值的影响(缺点)。要考虑对数据进行异常值处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d504d9d",
   "metadata": {},
   "source": [
    "3.1 特征缩放-对数据标准化,标准化的前提是数据必须要进行缺失值的处理，也就是要没有缺失值，这里上面已经处理了缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e6411e16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Fare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>712.000000</td>\n",
       "      <td>712.000000</td>\n",
       "      <td>712.000000</td>\n",
       "      <td>712.000000</td>\n",
       "      <td>712.000000</td>\n",
       "      <td>712.000000</td>\n",
       "      <td>712.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>448.589888</td>\n",
       "      <td>0.404494</td>\n",
       "      <td>2.240169</td>\n",
       "      <td>29.642093</td>\n",
       "      <td>0.514045</td>\n",
       "      <td>0.432584</td>\n",
       "      <td>34.567251</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>258.683191</td>\n",
       "      <td>0.491139</td>\n",
       "      <td>0.836854</td>\n",
       "      <td>14.492933</td>\n",
       "      <td>0.930692</td>\n",
       "      <td>0.854181</td>\n",
       "      <td>52.938648</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.420000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>222.750000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>8.050000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>445.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>28.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>15.645850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>677.250000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>33.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>891.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>80.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>512.329200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       PassengerId    Survived      Pclass         Age       SibSp  \\\n",
       "count   712.000000  712.000000  712.000000  712.000000  712.000000   \n",
       "mean    448.589888    0.404494    2.240169   29.642093    0.514045   \n",
       "std     258.683191    0.491139    0.836854   14.492933    0.930692   \n",
       "min       1.000000    0.000000    1.000000    0.420000    0.000000   \n",
       "25%     222.750000    0.000000    1.000000   20.000000    0.000000   \n",
       "50%     445.000000    0.000000    2.000000   28.000000    0.000000   \n",
       "75%     677.250000    1.000000    3.000000   38.000000    1.000000   \n",
       "max     891.000000    1.000000    3.000000   80.000000    5.000000   \n",
       "\n",
       "            Parch        Fare  \n",
       "count  712.000000  712.000000  \n",
       "mean     0.432584   34.567251  \n",
       "std      0.854181   52.938648  \n",
       "min      0.000000    0.000000  \n",
       "25%      0.000000    8.050000  \n",
       "50%      0.000000   15.645850  \n",
       "75%      1.000000   33.000000  \n",
       "max      6.000000  512.329200  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"注意，可以从打印的信息来观察是否需要做标准化，可以看每列的最小值和最大值，比如以下需要做标准化的列有['Age','SibSp','Parch','Fare']四列\n",
    " ，可以观察每一列的最小最大值，Age最小值为0.42，最大值为80，Fare的最小值为0，最大值为512，这两列属于取值范围较大的列，而SibSp最小值为0\n",
    "最大值为5，Parch最小值为0，最大值为6，这两列取值范围较大，取值范围较大的列和取值范围较小的列差异比较大，此时就需要做标准化\n",
    "\"\"\"\n",
    "df_drop_row.describe()   #查看数据详细信息 count:多少条数据 mean:均值 std:标准差 min、max：最小最大值  25%、50%、75%：四分位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6c3e582e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp',\n",
       "       'Parch', 'Ticket', 'Fare', 'Embarked'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_drop_row.columns"
   ]
  },
  {
   "cell_type": "raw",
   "id": "cec8a93c",
   "metadata": {},
   "source": [
    "#泰坦尼克号幸存顾客预测，通过泰坦尼克号中乘客的数据来预测未来发生船难时哪些人可以幸存，哪些人不能幸存\n",
    "'PassengerId',ID\n",
    "'Survived',是否幸存，0,1\n",
    "'Pclass', 船舱等级\n",
    "'Name',姓名\n",
    "'Sex',性别\n",
    "'Age',年龄\n",
    "'SibSp',带亲属的数量(子女、兄妹)\n",
    "'Parch', 带亲属的数量(父母)\n",
    "'Ticket',船票编号\n",
    "'Fare', 船票价格\n",
    "'Embarked'登录港口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0c1501b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler   #这个就是用来做标准化的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "75624f4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 三步走    1.定义规则   2.将规则应用到数据上    3.转型\n",
    "# 定义规则\n",
    "model_std = StandardScaler()\n",
    "# 将规则应用到数据上\n",
    "df_std = model_std.fit_transform(df_drop_row[['Age','SibSp','Parch','Fare']])  #特征缩放只能对数值类型的数据做处理\n",
    "# 转型\n",
    "df_std = pd.DataFrame(df_std,columns=['Age','SibSp','Parch','Fare'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1ae55802",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_drop_row[['Age','SibSp','Parch','Fare']] = df_std[['Age','SibSp','Parch','Fare']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8e5f7813",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PassengerId      0\n",
       "Survived         0\n",
       "Pclass           0\n",
       "Name             0\n",
       "Sex              0\n",
       "Age            148\n",
       "SibSp          148\n",
       "Parch          148\n",
       "Ticket           0\n",
       "Fare           148\n",
       "Embarked         0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_drop_row.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cae2e09e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Fare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>712.000000</td>\n",
       "      <td>712.000000</td>\n",
       "      <td>712.000000</td>\n",
       "      <td>564.000000</td>\n",
       "      <td>564.000000</td>\n",
       "      <td>564.000000</td>\n",
       "      <td>564.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>448.589888</td>\n",
       "      <td>0.404494</td>\n",
       "      <td>2.240169</td>\n",
       "      <td>0.004155</td>\n",
       "      <td>0.011589</td>\n",
       "      <td>0.029127</td>\n",
       "      <td>0.019436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>258.683191</td>\n",
       "      <td>0.491139</td>\n",
       "      <td>0.836854</td>\n",
       "      <td>1.018594</td>\n",
       "      <td>1.035980</td>\n",
       "      <td>1.033302</td>\n",
       "      <td>1.045999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.994931</td>\n",
       "      <td>-0.552714</td>\n",
       "      <td>-0.506787</td>\n",
       "      <td>-0.653427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>222.750000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.665764</td>\n",
       "      <td>-0.552714</td>\n",
       "      <td>-0.506787</td>\n",
       "      <td>-0.501257</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>445.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>-0.113383</td>\n",
       "      <td>-0.552714</td>\n",
       "      <td>-0.506787</td>\n",
       "      <td>-0.352868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>677.250000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>0.646142</td>\n",
       "      <td>0.522511</td>\n",
       "      <td>0.664747</td>\n",
       "      <td>0.017632</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>891.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.062809</td>\n",
       "      <td>4.823409</td>\n",
       "      <td>6.522419</td>\n",
       "      <td>9.031168</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       PassengerId    Survived      Pclass         Age       SibSp  \\\n",
       "count   712.000000  712.000000  712.000000  564.000000  564.000000   \n",
       "mean    448.589888    0.404494    2.240169    0.004155    0.011589   \n",
       "std     258.683191    0.491139    0.836854    1.018594    1.035980   \n",
       "min       1.000000    0.000000    1.000000   -1.994931   -0.552714   \n",
       "25%     222.750000    0.000000    1.000000   -0.665764   -0.552714   \n",
       "50%     445.000000    0.000000    2.000000   -0.113383   -0.552714   \n",
       "75%     677.250000    1.000000    3.000000    0.646142    0.522511   \n",
       "max     891.000000    1.000000    3.000000    3.062809    4.823409   \n",
       "\n",
       "            Parch        Fare  \n",
       "count  564.000000  564.000000  \n",
       "mean     0.029127    0.019436  \n",
       "std      1.033302    1.045999  \n",
       "min     -0.506787   -0.653427  \n",
       "25%     -0.506787   -0.501257  \n",
       "50%     -0.506787   -0.352868  \n",
       "75%      0.664747    0.017632  \n",
       "max      6.522419    9.031168  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_drop_row.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28588222",
   "metadata": {},
   "source": [
    "3.2 特征缩放-最小值最大值归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "419fa472",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import MinMaxScaler\n",
    "#MinMaxScaler:除了可以进行归一化外，也可以用于按比例对数据进行缩放的场景中\n",
    "\n",
    "# 三步走    1.定义规则   2.将规则应用到数据上    3.转型\n",
    "# 定义规则\n",
    "model_mm = MinMaxScaler(feature_range=(0,1))   #feature_range:缩放的取值范围，需要小的值在前，大的值在后 ,默认值就是(0,1)\n",
    "# 将规则应用到数据上\n",
    "df_mm = model_mm.fit_transform(df_drop_row[['Age','SibSp','Parch','Fare']])  #特征缩放只能对数值类型的数据做处理\n",
    "# 转型\n",
    "df_mm = pd.DataFrame(df_mm,columns=['Age','SibSp','Parch','Fare'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4d3e7535",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_drop_row[['Age','SibSp','Parch','Fare']] = df_mm[['Age','SibSp','Parch','Fare']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cb170e52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Fare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>712.000000</td>\n",
       "      <td>712.000000</td>\n",
       "      <td>712.000000</td>\n",
       "      <td>446.000000</td>\n",
       "      <td>446.000000</td>\n",
       "      <td>446.000000</td>\n",
       "      <td>446.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>448.589888</td>\n",
       "      <td>0.404494</td>\n",
       "      <td>2.240169</td>\n",
       "      <td>0.400182</td>\n",
       "      <td>0.107623</td>\n",
       "      <td>0.082212</td>\n",
       "      <td>0.073408</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>258.683191</td>\n",
       "      <td>0.491139</td>\n",
       "      <td>0.836854</td>\n",
       "      <td>0.201698</td>\n",
       "      <td>0.194959</td>\n",
       "      <td>0.150214</td>\n",
       "      <td>0.113881</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>222.750000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.276451</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.016896</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>445.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>0.372014</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.036634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>677.250000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>0.532423</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.076123</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>891.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       PassengerId    Survived      Pclass         Age       SibSp  \\\n",
       "count   712.000000  712.000000  712.000000  446.000000  446.000000   \n",
       "mean    448.589888    0.404494    2.240169    0.400182    0.107623   \n",
       "std     258.683191    0.491139    0.836854    0.201698    0.194959   \n",
       "min       1.000000    0.000000    1.000000    0.000000    0.000000   \n",
       "25%     222.750000    0.000000    1.000000    0.276451    0.000000   \n",
       "50%     445.000000    0.000000    2.000000    0.372014    0.000000   \n",
       "75%     677.250000    1.000000    3.000000    0.532423    0.200000   \n",
       "max     891.000000    1.000000    3.000000    1.000000    1.000000   \n",
       "\n",
       "            Parch        Fare  \n",
       "count  446.000000  446.000000  \n",
       "mean     0.082212    0.073408  \n",
       "std      0.150214    0.113881  \n",
       "min      0.000000    0.000000  \n",
       "25%      0.000000    0.016896  \n",
       "50%      0.000000    0.036634  \n",
       "75%      0.166667    0.076123  \n",
       "max      1.000000    1.000000  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_drop_row.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3d236b1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pip install scikit-learn==1.1.1 -i https://pypi.tuna.tsinghua.edu.cn/simple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9ab0e4eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pip list"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
